package com.src;

import java.sql.SQLException;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.src.model.Experience;
import com.src.model.User;

@Path("/experiences")
public class Experiences {
	// HTTP Get Method
	@GET 

	@Path("/doTagSearch")

	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") 

	public String doTagSearch(@QueryParam("tag") String tag, @QueryParam("type") String type,
			@QueryParam("numOfResults") String numberOfResults) throws Exception{
		String response = null;
		if(type.equals("2")){
			response = searchUsers(tag,numberOfResults);
		}else{
			response = searchExperiences(tag,type,numberOfResults);
		}
		return response;		
	}

	private String searchUsers(String tag, String numberOfResults) throws Exception {
		// TODO Auto-generated method stub
		List<String> userResult = DBConnection.searchUsers(tag,numberOfResults);
		JSONObject obj = new JSONObject();
		
		if(userResult == null){
			return Utitlity.constructJSON("0", "Internal System Error");
		} else {
			try {
				obj.put("status", new Boolean("true"));
				JSONArray ja = new JSONArray();
				for(int i=0;i<userResult.size();i++){
					JSONObject jo = new JSONObject();
					String temp[] = userResult.get(i).split(":"); 
 					//Experience input =  wsresult.get(i);
					jo.put("username"+i, temp[0]);
					jo.put("imageURL", temp[1]);
//					jo.put("body", input.getText());
//					jo.put("username", input.getCreatedUserName());
//					jo.put("experienceID", input.getID());
//					jo.put("helpfulCount", input.getHelpful());
//					jo.put("unhelpfulCount", input.getUnhelpful());
//					jo.put("spamCount", input.getSpam());
//					jo.put("latitude", input.getLatitude());
//					jo.put("longitude",input.getLongitude());
//					jo.put("exact",input.getExact());
					ja.put(jo);
				}
				obj.put("experiences", ja);
			} catch (JSONException e) {
				System.out.println("Json parse exception" + e.toString());
				return "not OK";
			}
		}
		
		return "OK";
	}

	@GET 
	@Path("/experienceInsert")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") 

	public String doExperienceInsert(@QueryParam("tag") String tag, @QueryParam("body") String body,
			@QueryParam("username") String username, @QueryParam("latitude") float latitude,
			@QueryParam("longitude") float longitude, @QueryParam("exact") String exact,
			@QueryParam("imageURL") String imageURL) throws Exception{
		String response = saveExperience(tag,body,username, latitude, longitude, exact, imageURL);

		return response;		
	}

	private String saveExperience(String tag, String body, String username, 
			float latitude, float longitude, String exact, String imageURL) throws Exception {
		Boolean result = null;
		try {
			result = DBConnection.saveExperience(tag, body, username, latitude, 
					longitude, exact, imageURL);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject obj = new JSONObject();
		if(result == null || result == false){
			return Utitlity.constructJSON("false", "Internal System Error");
		} else {
			return Utitlity.constructJSON("true", "");
		}
	}
//shttp://titan.cmpe.boun.edu.tr:8087/webservices/experience/doTagSearch?type=0&tag=..&numOfResults=..

	private String searchExperiences(String tag, String type, String nofResult) {
		List<Experience> wsresult = DBConnection.searchExperiences(tag,type,nofResult);
		JSONObject obj = new JSONObject();
		if(wsresult == null){
			return Utitlity.constructJSON("0", "Internal System Error");
		} else {
			try {
				obj.put("status", new Boolean("true"));
				JSONArray ja = new JSONArray();
				for(int i=0;i<wsresult.size();i++){
					JSONObject jo = new JSONObject();
					Experience input =  wsresult.get(i);
					jo.put("tag", input.getTags());
					jo.put("body", input.getText());
					jo.put("username", input.getCreatedUserName());
					jo.put("experienceID", input.getID());
					jo.put("helpfulCount", input.getHelpful());
					jo.put("unhelpfulCount", input.getUnhelpful());
					jo.put("spamCount", input.getSpam());
					jo.put("latitude", input.getLatitude());
					jo.put("longitude",input.getLongitude());
					jo.put("exact",input.getExact());
					jo.put("imageURL", input.getImageURL());
					ja.put(jo);
				}
				obj.put("experiences", ja);
			} catch (JSONException e) {
				System.out.println("Json parse exception" + e.toString());
			}
		}

		return obj.toString();
	}

}
